<template>
  <br>
  <el-table :data="tableuser" border style="width: 100%">
    <el-table-column prop="username" label="用户名" width="180" />
    <el-table-column prop="password" label="密码" width="180" />
    <el-table-column prop="phone" label="手机号码" />
    <el-table-column label="操作">
      <template #default="scope">
        <el-button size="small" @click="tableEditHandler(scope.$index, scope.row)">
          <el-icon><Edit /></el-icon>编辑
        </el-button>
        <el-button size="small" type="danger" @click="tableDeleteHandler(scope.$index, scope.row)">
          <el-icon><Delete /></el-icon>删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  
  <el-pagination
      :current-page="currentPage"
      :page-size="pageSize.value"   
      :total="total"
      @current-change="handlePageChange"
      @size-change="handleSizeChange"
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10, 20, 30, 40, 50]"
      style="margin-top: 20px; text-align: right;"
    />
  
  <el-dialog title="编辑用户" v-model="editDialogVisible">
    <el-form :model="editForm">
      <el-form-item label="用户名">
        <el-input v-model="editForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="editForm.password"></el-input>
      </el-form-item>
      <el-form-item label="邮箱号码">
        <el-input v-model="editForm.phone"></el-input>
      </el-form-item>
    </el-form>
    <span class="dialog-footer">
      <el-button @click="editDialogVisible = false">取消</el-button>
      <el-button type="primary" @click="tableUpdateHandler">保存</el-button>
    </span>
  </el-dialog>
</template>

<script setup>
import request from '@/utils/request'
import { onMounted, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus';

const editDialogVisible = ref(false)
const tableuser = ref([])
const editForm = ref({
  id: '',
  username: '',
  password: '',
  phone: ''
})

const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

const tableEditHandler = (rowIndex, rowData) => {
  editForm.value = { ...rowData }
  editDialogVisible.value = true
}

const handlePageChange = (page) => {
  currentPage.value = page
  gettableuser()
}

const handleSizeChange = (size) => {
  pageSize.value = size
  gettableuser()
}

onMounted(() => {
  gettableuser()
})

const gettableuser = () => {
  request({
    url: '/sys/user/gets',
    method: 'GET',
    params: {
      page: currentPage.value,
      pageSize: pageSize.value
    }
  }).then(req => {
    if (200 == req.data.errorCode) {
      tableuser.value = req.data.data.records
      total.value = req.data.data.total
    }
  })
}

const tableDeleteHandler = (rowIndex, rowData) => {
  ElMessageBox.confirm(
    '确认删除用户 : ' + rowData.username,
    'Warning',
    {
      confirmButtonText: '删除',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      request({
        method: 'delete',
        url: '/sys/user/del/' + rowData.id,
      }).then((reg) => {
        if (200 == reg.data.errorCode) {
          gettableuser();
          ElMessage({
            type: 'success',
            message: '删除成功',
          });
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      });
    });
};

const tableUpdateHandler = () => {
  request({
    url: '/sys/user/update',
    method: 'PUT',
    data: editForm.value
  }).then(res => {
    if (200 == res.data.errorCode) {
      ElMessage({
        type: 'success',
        message: '更新成功'
      })
      editDialogVisible.value = false
      gettableuser()
    } else {
      ElMessage({
        message: '不允许修改用户名',
        type: 'error',
      })
    }
  })
}
</script>
